﻿// =================================================================== 
// 项目说明
//====================================================================
// 文件： ERP_CusCargo.cs
// 创建时间：2016/11/26
// 负责人：
// ===================================================================
using System;
using System.Data;
using CRMMis.Model;
using CRMMis.IDAL;
using CRMMis.DALFactory;

namespace CRMMis.BLL
{
    /// <summary>
    ///  商品目录表 的BLL层
    /// </summary>
    public class ERP_CusCargo : ERP_CusCargoBLLBase
    {
		#region ----------变量定义----------
		#endregion
		
		#region ----------构造函数----------
		/// <summary>
		/// 默认构造函数
		/// </summary>
		public ERP_CusCargo()
		{
		}
        #endregion

        #region----------函数定义---------
        public static void DeletedCargo(string Id, string DelUser)
        {
            CommonLib.BLL.MyTransaction mytrans = new CommonLib.BLL.MyTransaction();
            try
            {
                mytrans.BeginTransaction();
                Model.ERP_CusCargo model = ERP_CusCargo.GetModel(int.Parse(Id));
                int state = model.State;
                model.State = -1;
                ERP_CusCargo.Update(mytrans, model);
                if (model.State == 0)
                {
                    Model.ERP_OperationLog omodel = new Model.ERP_OperationLog();
                    omodel.LogId = ERP_OperationLog.GetNewID();
                    omodel.TableName = "ERP_CusCargo";
                    omodel.Action = "Delete";
                    omodel.ActionUser = DelUser;
                    omodel.RecordID = int.Parse(Id);
                    omodel.ActionDate = System.DateTime.Now.ToString();
                    omodel.ActionRecord = "记录号为" + model.CargoId + ",商品名为" + model.CargoName + "的商品被删除。";
                    ERP_OperationLog.Add(mytrans, omodel);
                }
                mytrans.CommitTran();
            }
            catch (Exception ex)
            {
                mytrans.RollBackTran();
                throw new Exception(ex.Message);
            }
        }

        public static string Update(Model.ERP_CusCargo oModel, Model.ERP_CusCargo_Detail dModel, int old_DetailId, string type)
        {
            CommonLib.BLL.MyTransaction mytrans = new CommonLib.BLL.MyTransaction();
            try
            {
                mytrans.BeginTransaction();
                Model.ERP_CusCargo_Detail model = new Model.ERP_CusCargo_Detail();
                if (type == "add")
                {
                    oModel.CargoId = ERP_CusCargo.GetNewID();
                    dModel.CargoId = oModel.CargoId;
                    dModel.DetailId = ERP_CusCargo_Detail.GetNewID();
                    ERP_CusCargo.Add(mytrans, oModel);
                    ERP_CusCargo_Detail.Add(mytrans, dModel);
                }
                else if (type == "addD")
                {
                    //dModel.CargoId = oModel.CargoId;
                    //dModel.DetailId = ERP_CusCargo_Detail.GetNewID();
                    ERP_CusCargo_Detail.Update(mytrans, dModel);
                }
                else
                {
                    if (type != "submit")
                    {
                        ERP_CusCargo.Update(mytrans, oModel);
                        ERP_CusCargo_Detail.Update(mytrans, dModel);
                    }
                    else
                    {
                        if (old_DetailId != 0 && type == "submit")
                        {
                            model = ERP_CusCargo_Detail.GetModel(old_DetailId);
                            model.State = 2;
                            ERP_CusCargo_Detail.Update(mytrans, model);
                            Model.ERP_OperationLog olmodel = new Model.ERP_OperationLog();
                            olmodel.LogId = ERP_OperationLog.GetNewID();
                            olmodel.TableName = "ERP_Cargo_Detail";
                            olmodel.Action = "Edit";
                            olmodel.ActionUser = oModel.InUser;
                            olmodel.RecordID = old_DetailId;
                            olmodel.ActionDate = oModel.InDate;
                            olmodel.ActionRecord = "修改记录号为" + old_DetailId + "商品编号为" + model.CargoId + ",商品名为" + oModel.CargoName + "的供应信息的供应状态。";
                        }
                        dModel.CargoId = oModel.CargoId;
                        ERP_CusCargo.Update(mytrans, oModel);
                        ERP_CusCargo_Detail.Update(mytrans, dModel);
                        Model.ERP_OperationLog Omodel = new Model.ERP_OperationLog();
                        Omodel.LogId = ERP_OperationLog.GetNewID();
                        Omodel.TableName = "ERP_Cargo";
                        Omodel.Action = "Edit";
                        Omodel.ActionUser = oModel.InUser;
                        Omodel.RecordID = oModel.CargoId;
                        Omodel.ActionDate = oModel.InDate;
                        Omodel.ActionRecord = "修改记录号为" + model.CargoId + ",商品名为" + oModel.CargoName + "的商品。";
                        Model.ERP_OperationLog omodel = new Model.ERP_OperationLog();
                        omodel.LogId = ERP_OperationLog.GetNewID();
                        omodel.TableName = "ERP_Cargo_Detail";
                        omodel.Action = "Add";
                        omodel.ActionUser = oModel.InUser;
                        omodel.RecordID = dModel.DetailId;
                        omodel.ActionDate = oModel.InDate;
                        omodel.ActionRecord = "新增记录号为" + dModel.DetailId + "商品编号为" + model.CargoId + ",商品名为" + oModel.CargoName + "的供应信息。";
                    }

                }
                mytrans.CommitTran();
                return "";
            }
            catch (Exception ex)
            {
                var err = ex.Message;
                mytrans.RollBackTran();
                return err;
            }
        }

        public static DataSet GetCargoList(string sWhere, string sOder)
        {
            var lst = _dal.GetCargoList(sWhere, sOder);
            return lst;
        }

        #endregion
    }
}